package com.spddr.pmos.rpt.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Period;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.spddr.pmos.domain.Metrics;
import com.spddr.pmos.domain.TreeLevel;
import com.spddr.pmos.jpa.River;
import com.spddr.pmos.repos.RainStationRepos;
import com.spddr.pmos.repos.RiverRepos;
import com.spddr.pmos.rpt.RptRainStation;
import com.spddr.pmos.rpt.pojo.CellValue;

import cn.seqdata.echarts.Option;
import cn.seqdata.echarts.bmap.BMap;
import cn.seqdata.echarts.data.MapData;
import cn.seqdata.echarts.series.Heatmap;
import cn.seqdata.tags.Point;
import cn.seqdata.tags.RegularValues;
import cn.seqdata.tags.common.IdTags;

@Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class RptRainStationImpl extends AbstractPivotChartImpl implements RptRainStation {

	@Autowired
	private RiverRepos riverRepos;

	@Autowired
	private RainStationRepos rainStationRepos;

	protected Period intervalOfStorage() {
		return Days.ONE.equals(interval) ? Hours.ONE.toPeriod() : Days.ONE.toPeriod();
	}

	@Override
	public List<CellValue> loadPivot() {
		return rainStationRepos.findAll().stream().flatMap(s -> {
			// RegularValues<IdTags, Point<IdTags>> r = load(TreeLevel.Rain.id,
			// s.getId(), Metrics.RAIN.name);
			Point<IdTags> point = new Point<>(metric, new IdTags(id));
			return sim(1).getValue().getValues().stream().map(v -> new CellValue(point, v, null, s.getName()));
		}).collect(Collectors.toList());
	}

	@Override
	public Option chart() {
		Option option = new Option();

		River river = riverRepos.getOne(id);
		// 河流的流域中心位置
		BMap bmap = new BMap(102.695408, 29.319237, 12);
		option.bmap(bmap);

		List<Double[]> points = rainStationRepos.findAll().stream().map(x -> new Double[] { x.getLng(), x.getLat() })
				.collect(Collectors.toList());
		MapData mapData = new MapData("雨量站坐标", points);

		Heatmap series = new Heatmap("");
		series.data().add(mapData);
		option.series().add(series);

		option.title().subtext(river.getName() + "流域降雨量分布图");

		return option;
	}

}
